
$(function () {
    let userinfo = JSON.parse(localStorage.getItem("userinfo" || '[]'));
    if (userinfo) {
        $(".list2 .denglu").hide();
        $(".list2 .zhuce").hide();
        $(".list2 .usename").show();
        $(".list2 .exit").show();
        $(".list2 .usename").text(`欢迎${userinfo[0].u_nickname},回来`);
    }
    $(".list2 .exit").on("click", function () {
        localStorage.clear();
        $(".list2 .denglu").show();
        $(".list2 .zhuce").show();
        $(".list2 .usename").hide();
        $(".list2 .exit").hide();
    })
})








$(function () {
    var total = function () {
        var sum = 0;
        for (var i = 0; i < $(".ss .rmb").length; i++) {
            var money = $(".ss .rmb").eq(i).html().trim()
            var num = Number(money.slice(1, money.length));
            sum += num;
        }
        $(".check .current").parent(".check").siblings(".rmb").each(function (index, el) {
            let step = $(el).html().trim();
            let st = Number(step.slice(1, money.length))
            sum -= st;
        })
        $(".topay .now span").html("￥" + sum);
    }
    var sumnum = function () {
        var sum = 0;
        for (var i = 0; i < $(".ss .num").length; i++) {
            var str = $(".ss .num").eq(i).html().trim()
            var num = Number(str);
            sum += num;
        }
        $(".check .current").parent(".check").parent("tr").find(".num").each(function (index, el) {
            let st = Number($(el).html().trim())
            console.log(st);
            sum -= st;
        })
        $(".topay .sum .num").html(sum + "件")
    }
    sumnum();
    total();



    $.ajax({
        url: "./cart/query",
        type: "get",
        data: { uId: JSON.parse(localStorage.getItem("userinfo"))[0].u_id }
    }).then(function (res) {
        layer.msg(res.msg)
        nn = res.data
        render(res.data)
    })


    function render(list) {
        let strhtml = '';
        let nn = 0
        list.forEach(el => {
            nn += el.c_total
            strhtml += `<tr class="ss" data-cid=${el.c_id}>`;
            strhtml += ` <td class="check"><span></span></td>`;
            strhtml += ` <td class="goods1 clear"><img src=${el.c_img} alt="">`;
            strhtml += `<div class="cartname"><span class="p1">${el.c_name}</span>`;
            strhtml += ` <span class="p2">更换促销 ﹀ </span>`;
            strhtml += `</div></td><td>分类:${el.c_class}</td>`;
            strhtml += ` <td class="dj">￥${el.c_price}</td><td>`;
            strhtml += `<span class="jian">-</span><span class="num">${el.c_num}</span><span class="jia">+</span>`;
            strhtml += ` </td><td class="rmb">￥${el.c_total}</td>`;
            strhtml += ` <td class="del"><span>删除</span></td></tr>`;
        })
        if (nn >= 299) {
            let strhtmlstar = '';
            strhtmlstar += '<tr class="info"><td class="zeng" colspan="7"><span class="p1">满赠</span><span class="p2">';
            strhtmlstar += '已满299元，可选赠品1件</span><span class="p3">重选赠品 ></span></td></tr>';
            let strhtmlend = '';
            strhtmlend += ' <tr class="s2 ss"><td class="check"></td><td class="goods1 clear">';
            strhtmlend += ' <img src="./img/gwc2.png" alt=""><div class="cartname"><span class="p1">【满赠】</span>';
            strhtmlend += '  <span class="p2">诺棉 75%消毒湿巾80抽/包</span></div></td><td>';
            strhtmlend += ' 分类:80抽/包</td><td>¥0.00</td><td>';
            strhtmlend += ' x1</td><td class="rmb">¥0.00</td><td></td></tr>';
            $("tbody").html(strhtmlstar + strhtml + strhtmlend);
            sumnum();
            total();
        } else {
            $("tbody").html(strhtml);
            sumnum();
            total();
        }
        return 1
    }











    //选择框
    //checkbox
    $("tbody").on("click", ".ss .del span", async function () {
        let self = this;
        let cid = $(this).parent("td").parent("tr").data("cid");
        await layer.confirm("你确定要删除吗", {
            btn: ["确定", "取消"]
        }, function (index) {
            $.ajax({
                url: "./cart/delete",
                type: "post",
                data: { cid }
            }).done(function (res) {
                layer.msg(res.msg);
                if (res.status == 200) {
                    $(self).parent("td").parent("tr").remove();
                    $.ajax({
                        url: "./cart/query",
                        type: "get",
                        data: { uId: JSON.parse(localStorage.getItem("userinfo"))[0].u_id }
                    }).then(function (res) {
                        layer.msg(res.msg)
                        nn = res.data
                        render(res.data)
                    })
                }
            })
        }, function () {
        })
        // $(this).parent("td").parent("tr").remove();
        total();
        sumnum();
    })

    document.querySelectorAll(".ss .check span").forEach(function (el, index) {
        el.tempindex = true;
    })
    $("tbody").on("click", ".ss .check span", function () {
        $(this)[0].tempindex = !$(this)[0].tempindex;
        if ($(this)[0].tempindex) {
            $(this).removeClass("current")
        } else {
            $(this).addClass("current")
        }
        var flag = true
        Array.from(document.querySelectorAll(".ss .check span")).forEach(function (el, index) {
            if (el.tempindex == false) {
                flag = false
            }
        })
        // console.log(flag);
        if (flag == false) {
            $(".tables .qx span").css({
                "background": " url(./img/nocheck.png) no-repeat center left",
            })
            $(".console .p1").css({
                "background": "url(./img/nocheck.png) no-repeat left center"
            })
        } else {
            $(".tables .qx span").css({
                "background": " url(./img/checkbox.png) no-repeat center left",
            })
            $(".console .p1").css({
                "background": "url(./img/checkbox.png) no-repeat left center"
            })
        }
        sumnum();
        total();


    })
    var flag = true;
    $(".console .p1").on("click", function () {
        flag = !flag;
        if (flag) {
            $(".ss .check span").removeClass("current")
            $(this).css({
                "background": "url(./img/checkbox.png) no-repeat left center"
            })
            $(".tables .qx span").css({
                "background": " url(./img/checkbox.png) no-repeat center left",
            })
        }
        else {
            $(".ss .check span").addClass("current")
            $(this).css({
                "background": "url(./img/nocheck.png) no-repeat left center"
            })
            $(".tables .qx span").css({
                "background": " url(./img/nocheck.png) no-repeat center left",
            })
        }
        sumnum();
        total();
    })

    //数量加减
    $("tbody").on("click", ".jian", function () {
        let num = Number($(this).parent("td").children(".num").html());
        let rmb = $(this).parent("td").parent("tr").children(".dj").html().trim();
        num--;
        if (num < 1) {
            num = 1;
        }
        $(this).parent("td").parent("tr").children(".rmb").html("￥" + rmb.slice(1, rmb.length) * num)
        $(this).parent("td").children(".num").html(num);
        $.ajax({
            url: "./cart/modify",
            type: "post",
            data: {
                cid: $(this).parents("td").parents("tr").data("cid"),
                cnum: num
            }
        })
        total();
        sumnum();
    })
    $("tbody").on("click", ".jia", function () {
        var num = Number($(this).parent("td").children(".num").html())
        var rmb = $(this).parent("td").parent("tr").children(".dj").html().trim();
        num++;
        $(this).parent("td").parent("tr").children(".rmb").html("￥" + rmb.slice(1, rmb.length) * num)
        $(this).parent("td").children(".num").html(num);
        $.ajax({
            url: "./cart/modify",
            type: "post",
            data: {
                cid: $(this).parents("td").parents("tr").data("cid"),
                cnum: num
            }
        })
        total();
        sumnum();
    })



})

$(function () {

})







